﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;
using FViewport = Freeform.Viewport;

#endregion


namespace Freeform
{
    /// <summary>
    /// The viewport class represents a section of the game-screen that a scene can be rendered to.
    /// </summary>
    public class Viewport2D : FViewport, IGeometric2D
    {

        #region Members

        /// <summary>
        /// Gets the camera for this viewport.
        /// </summary>
        public Camera2D Camera { get; internal set; }


        /// <summary>
        /// Gets the position in World units that the mouse is over, using this viewport.
        /// </summary>
        public virtual Vector2 MouseLocation_World
        {
            get { return Camera.Position + Camera.Size * MouseLocation_View; }
        }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new 2d viewport.
        /// </summary>
        /// <param name="World">The world that the viewport is looking at.</param>
        public Viewport2D(World World)
            : base(World)
        {
            new Camera2D(this);
        }

        #endregion


        #region Update


        #endregion


        #region Draw

        /// <summary>
        /// Allows the viewport to draw itself.
        /// </summary>
        /// <param name="Time">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime Time)
        {
            base.Draw(Time);

            // Start the spritebatch.
            SpriteBatch.Begin(SpriteSorting, BlendState.AlphaBlend);

            // Render each entity.
            for (int i = 0; i < World.ComponentList.Loaded.Count; i++)
            {
                Entity2D Entity = World.ComponentList.Loaded[i] as Entity2D;
                if (Entity != null && Entity.IsVisible && !Entity.IsDisposed)
                    Entity.Draw(Time, this);
            }

            // Render debug text.
            if (DebugText != null && DebugFont != null)
                SpriteBatch.DrawString(DebugFont, DebugText, new Vector2(5, 5), DebugColour);

            // End scene.
            SpriteBatch.End();
        }

        #endregion


        #region Dispose

        /// <summary>
        /// Disposes this viewport and removes it from the game.
        /// </summary>
        protected override void OnDispose()
        {
            base.OnDispose();

            // Dispose spritebatch.
            SpriteBatch.Dispose();

            // Dispose the camera.
            Camera.Dispose();
        }

        #endregion

    }
}